﻿@model EstimateShippingModel
@using Nop.Web.Models.ShoppingCart;
@if (Model.Enabled)
{
    <div class="shipping">
        <script type="text/javascript">
            $(function () {
                $("#@Html.FieldIdFor(model => model.CountryId)").change(function () {
             var selectedItem = $(this).val();
             var ddlStates = $("#@Html.FieldIdFor(model => model.StateProvinceId)");
                var estimateProgress = $("#estimate-shipping-loading-progress");
                estimateProgress.show();
                $.ajax({
                    cache: false,
                    type: "GET",
                    url: "@(Url.RouteUrl("GetStatesByCountryId"))",
                    data: { "countryId": selectedItem, "addEmptyStateIfRequired": "true" },
                    success: function (data) {
                        ddlStates.html('');
                        $.each(data, function (id, option) {
                            ddlStates.append($('<option></option>').val(option.id).html(option.name));
                        });
                        estimateProgress.hide();
                    },
                    error: function (xhr, ajaxOptions, thrownError) {
                        alert('Failed to retrieve states.');
                        estimateProgress.hide();
                    }
                });
            });
     });
        </script>

        <div class="estimate-shipping">
            <div class="title">
                <strong>@T("ShoppingCart.EstimateShipping")</strong>
            </div>
            <div class="hint">@T("ShoppingCart.EstimateShipping.Tooltip")</div>
            <div class="shipping-options">
                <div>
                    <table>
                        <tr>
                            <td>
                                @Html.LabelFor(model => model.CountryId):
                            </td>
                            <td>
                                @Html.DropDownListFor(model => model.CountryId, Model.AvailableCountries, new { @class = "country-input" })
                                @Html.ValidationMessageFor(model => model.CountryId)
                            </td>
                        </tr>
                        <tr>
                            <td>
                                @Html.LabelFor(model => model.StateProvinceId):
                            </td>
                            <td>
                                @Html.DropDownListFor(model => model.StateProvinceId, Model.AvailableStates, new { @class = "state-input" })
                                <span id="estimate-shipping-loading-progress" style="display: none;" class="please-wait">@T("Common.Wait...")"</span>
                                @Html.ValidationMessageFor(model => model.StateProvinceId)
                            </td>
                        </tr>
                        <tr>
                            <td>
                                @Html.LabelFor(model => model.ZipPostalCode):
                            </td>
                            <td>
                                @Html.TextBoxFor(model => model.ZipPostalCode, new { @class = "zip-input" })
                                @Html.ValidationMessageFor(model => model.ZipPostalCode)
                            </td>
                        </tr>
                        <tr>
                            <td></td>
                            <td>
                                <input type="submit" name="estimateshipping" value="@T("ShoppingCart.EstimateShipping.Button")" class="button-2 estimate-shipping-button" />
                            </td>
                        </tr>
                    </table>
                </div>
                <div class="clear">
                </div>
                <div>
                    @foreach (var shippingOption in Model.ShippingOptions)
                    {
                        <div class="shipping-option-item">
                            <div class="option-name">
                                @shippingOption.Name (@shippingOption.Price)
                            </div>
                            <div class="option-description">
                                @Html.Raw(shippingOption.Description)
                            </div>
                        </div>
                    }
                </div>
                <div class="clear">
                </div>
                @if (Model.Warnings.Count > 0)
                {
                    <div class="message-error">
                        <ul>
                            @for (int i = 0; i < Model.Warnings.Count; i++)
                            {
                                <li>@Model.Warnings[i]</li>
                            }
                        </ul>
                    </div>
                }
            </div>
        </div>
    </div>
}